package com.flyme.common.core.model.query;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flyme.common.core.utils.ObjectUtils;
import com.google.common.collect.Lists;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zyf
 * @date: 2018/9/4 8:42
 * @@desc: 查询构造器
 */
public class CriteriaQuery<T> extends QueryWrapper<T> {

    private static final long serialVersionUID = 1L;
    /**
     * 外键表别名对象
     */
    protected Map<String, String> aliasMap = new HashMap<>();
    /**
     * 查询字段
     */
    protected List<String> select = Lists.newArrayList();
    /**
     * 分页对象
     */
    private PagerInfo pagerInfo;


    public CriteriaQuery() {

    }

    public CriteriaQuery(PagerInfo pagerInfo) {
        if (ObjectUtil.isNotNull(pagerInfo.getSqlfields())) {
            this.select.add(pagerInfo.getSqlfields());
        }
        this.pagerInfo = pagerInfo;
        pagerInfo.setCurrent(pagerInfo.getPageNumber());
        pagerInfo.setSize(pagerInfo.getPageSize());
        String sort = pagerInfo.getSort();
        apply("1=1");
        if (StrUtil.isNotBlank(sort)) {
            String order = pagerInfo.getOrder();
            Boolean isAsc = Sort.asc.toString().equals(order) ? true : false;
            orderBy(true, isAsc, sort);
        }
    }

    public Map<String, String> getAliasMap() {
        return aliasMap;
    }

    /**
     * 创建外键表关联对象,需要在mapper(xml)中编写join
     */
    public void createAlias(String entiry, String alias) {
        this.aliasMap.put(entiry, alias);
    }

    /**
     * 等于
     */
    @Override
    public CriteriaQuery<T> eq(String column, Object val) {
        super.eq(ObjectUtils.isNotEmpty(val) && !val.equals(-1) && !val.equals(-1L), column, val);
        return this;
    }

    /**
     * like
     */
    @Override
    public CriteriaQuery<T> like(String column, Object val) {
        like(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    /**
     * in
     */
    @Override
    public CriteriaQuery<T> in(String column, Object... val) {
        in(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }



    /**
     * ge
     */
    @Override
    public CriteriaQuery<T> ge(String column, Object val) {
        ge(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    /**
     * le
     */
    @Override
    public CriteriaQuery<T> le(String column, Object val) {
        le(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    /**
     * lt
     */
    @Override
    public CriteriaQuery<T> lt(String column, Object val) {
        lt(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    /**
     * gt
     */
    @Override
    public CriteriaQuery<T> gt(String column, Object val) {
        gt(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }


    /**
     * or
     */
    @Override
    public CriteriaQuery<T> or() {
        super.or();
        return this;
    }

    /**
     * likeLeft
     */
    @Override
    public QueryWrapper<T> likeLeft(String column, Object val) {
        return likeLeft(ObjectUtils.isNotEmpty(val), column, val);
    }

    /**
     * likeRight
     */
    @Override
    public QueryWrapper<T> likeRight(String column, Object val) {
        return likeRight(ObjectUtils.isNotEmpty(val), column, val);
    }

    public CriteriaQuery<T> addSql(String sql) {
        this.select.add(sql);
        return this;
    }

    public PagerInfo getPagerInfo() {
        return pagerInfo;
    }

    public void setPagerInfo(PagerInfo pagerInfo) {
        this.pagerInfo = pagerInfo;
    }

    public String getSelect() {
        if (CollectionUtil.isNotEmpty(select)) {
            return String.join(",", select);
        } else {
            return null;
        }
    }

}
